package org.sbbs.app.license.webapp.action;

import org.sbbs.app.license.model.LicenseInfo;
import org.sbbs.app.license.service.CustomerInfoManager;
import org.sbbs.app.license.service.LicenseInfoManager;
import org.sbbs.base.webapp.action.BaseMaintainAction;

public class LicenseInfoMaintainAction
    extends BaseMaintainAction<LicenseInfo, Long> {
    /**
     *
     */
    private static final long serialVersionUID = 4898653670229709972L;

    private Long customerId;

    private LicenseInfo model;

    private LicenseInfoManager licenseInfoManager;

    private CustomerInfoManager customerInfoManager;

    public String edit() {
        this.model = this.licenseInfoManager.find( this.getId() );

        this.setEditTypeEdit();
        return SUCCESS;
    }

    public String add() {
        this.model = new LicenseInfo();
        model.setCustomerInfo( customerInfoManager.find( customerId ) );
        this.setEditTypeAdd();
        return SUCCESS;
    }

    public String save() {
        try {
            // HttpServletRequest req = this.getRequest();
            // boolean isNew = ( this.getEditType() == 1 );
            this.licenseInfoManager.save( this.getModel() );
            this.setAjaxMessage( getText( ( isNew() ) ? "licenseInfo.added" : "licenseInfo.updated", "no msg key found,save successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.saved", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }
        return SUCCESS;
    }

    public String delete() {
        try {
            String[] sIds = this.getIds().split( "," );

            Long[] lIds = new Long[sIds.length];
            for ( int i = 0; i < sIds.length; i++ ) {
                lIds[i] = Long.parseLong( sIds[i] );
            }

            this.licenseInfoManager.removeByIds( lIds );

            this.setAjaxMessage( getText( "licenseInfo.deleted", "no msg key found,delete successed." ) );
            this.setAjaxStatus( AJAX_STATUS_SUCCESS );
        }
        catch ( Exception e ) {
            this.setAjaxMessage( getText( "error.deleted", new String[] { e.getMessage() } ) );
            this.setAjaxStatus( AJAX_STATUS_ERROR );
        }

        return SUCCESS;
    }

    public LicenseInfo getModel() {
        return model;
    }

    public void setModel( LicenseInfo model ) {
        this.model = model;
    }

    public LicenseInfoManager getLicenseInfoManager() {
        return licenseInfoManager;
    }

    public void setLicenseInfoManager( LicenseInfoManager licenseInfoManager ) {
        this.licenseInfoManager = licenseInfoManager;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId( Long customerId ) {
        this.customerId = customerId;
    }

    public CustomerInfoManager getCustomerInfoManager() {
        return customerInfoManager;
    }

    public void setCustomerInfoManager( CustomerInfoManager customerInfoManager ) {
        this.customerInfoManager = customerInfoManager;
    }

}
